<template>
    <div class="content">
        <nut-navbar class="navbar" z-index="5" :clickable="false"  fixed safe-area-inset-top>
          <template #content>
            <span>{{ t(String(route.meta.title)) }}</span>
          </template>
          <template #left>
            <img src="/imgs/club/arrow_left.png" alt="" @click="router.go(-1)" class="arrow_left" />
          </template>
          <template #right>
            <span class="save" @click="router.push('/club/member/approvalMenber')">成员审批</span>
          </template>
        </nut-navbar>

      <!-- 搜索 -->
      <div class="search_box">
        <nut-searchbar class="search_input" placeholder="搜索成员ID或账号" v-model="searchVal"
          @change="onClickSearch" shape="square" background="transparent" input-background="#1D1C3C">
          <template #leftin>
            <iconpark-icon name="sousuo" style="margin-right: 14px;" size="14" />
          </template>
        </nut-searchbar>
        <span class="search_btn">搜索</span>
      </div>

      <!-- 成员 -->
      <div class="member_container">
        <div
          v-for="(member, index) in members"
          :key="member.id"
          class="member_item"
        >
          <div class="member_info">
            <img :src="member.avatar" alt="头像" class="member_avatar" />
            <div class="info">
              <div class="name_section">
                <p class="member_name">{{ member.name }}</p>
                <span
                  v-if="member.role"
                  class="tag"
                  :class="{
                    creator: member.role === 'creator',
                    admin: member.role === 'admin'
                  }"
                >
                  <em class="tag_text">{{ member.role === 'creator' ? '创建者' : (member.role === 'admin' ? '管理员' : '')  }}</em>
                </span>
              </div>
              <span class="vip_tag" :class="'vip_' + member.vipLevel">
                <!-- VIP{{ member.vipLevel }} -->
                <ImgT :src="member.vipLevel" alt="" />
              </span>
            </div>
          </div>
          <div class="member_actions">
            <span
              v-if="member.role === 'admin'"
              class="action_btn"
              @click="updateRole(index, 'normal', member.name)"
            >
              取消管理员
            </span>
            <span
              v-if="member.role !== 'admin' && member.role !== 'creator'"
              class="action_btn"
              @click="updateRole(index, 'admin', member.name)"
            >
              设为管理员
            </span>
            <span class="action_btn" v-if="member.role !== 'creator'" @click="removeMember(index, member.name)">踢出</span>
          </div>
        </div>
      </div>

      <Modal v-model:visible="isShowWin" :title="t(club_title)" @close="">
        <div class="popup_body">
          <div class="info">{{ club_tip }}</div>
          <div class="submit_btn">
            <span class="submit_close" @click="close">取消</span>
            <Btn height="44px" width="147px" class="submit" @click="submitAc">确定</Btn>
          </div>
        </div>
      </Modal>
    </div>
  </template>
  
  <script setup lang="ts">
  import { useI18n } from "vue-i18n";
  import { ref, reactive } from "vue";
  import {useRoute, useRouter} from "vue-router";
  
  const route = useRoute();
  const router = useRouter();
  const { t } = useI18n();

  const searchVal = ref('')
  const searchValFalg = ref(false)

  // 搜索
  const onClickSearch = () => {
    if (!searchVal.value) {

    }
    searchValFalg.value = true
  }

  // 成员列表
  const members = reactive([
    {
      id: 1,
      name: "Frank002",
      avatar: "/imgs/club/member_avatar.png",
      vipLevel: "/imgs/club/vip.png",
      role: "creator",
    },
    {
      id: 2,
      name: "Frank002",
      avatar: "/imgs/club/member_avatar.png",
      vipLevel: "/imgs/club/vip.png",
      role: "admin",
    },
    {
      id: 3,
      name: "Frank002",
      avatar: "/imgs/club/member_avatar.png",
      vipLevel: "/imgs/club/vip.png",
      role: "normal",
    },
  ]);

  const isShowWin = ref(false);
  const club_title = ref('');
  const club_tip = ref()
  const memberIndex = ref(0)
  const memberRole = ref('')
  // 更新角色
  const updateRole = (index: number, role: "admin" | "normal", name: any) => {
    club_tip.value =  role == 'admin' ? `是否将${name}设置为管理员` : `是否${name}将的管理员取消`
    isShowWin.value = true;
    memberIndex.value = index;
    memberRole.value = role;
  };

  // 移除成员
  const removeMember = (index: number, name: any) => {
    club_tip.value = `是否将${name}踢出俱乐部`;
    memberIndex.value = index;
    isShowWin.value = true;
  };

  const submitAc = () => {
    if (members[memberIndex.value].role === "creator") return; // 创建者无法更改角色
    members[memberIndex.value].role = memberRole.value;
    if(members[memberIndex.value].role !== "creator") {
      members.splice(memberIndex.value, 1)
    }
    isShowWin.value = false
  };

  const close = () => {
    isShowWin.value = false
  };
  

  </script>
  
  <style scoped lang="scss">
  .content {
    font-size: 14px;
    margin: 0 0 10px;

    .arrow_left {
      width: 24px;
      height: 24px;
    }

    .save {
      height: 32px;
      line-height: 32px;
      text-align: center;
      color: #5BD9FF;
    }

    .search_box {
      // width: 347px;
      margin: 20px auto 0;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .search_input {
      
      :deep(.square) {
        border-radius: 8px;
        height: 44px;
        box-shadow: 0.5px 0.5px 1px 0px #2E296B inset;
      }
    }

    .search_btn {
      width: 82px;
      height: 44px;
      margin-right: 14px;
      font-size: 14px;
      line-height: 44px;
      text-align: center;
      color: #FFFFFF;
      background: linear-gradient(180deg, #5567FF 0%, #9E1EFF 100%);
      box-shadow: 0.5px 0.5px 1px 0px #9B9EFF inset;
      border-radius: 8px;
    }

    .member_container {
      // padding: 20px;
      width: 347px;
      margin: 0 auto;

      .member_item {
        height: 62px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        background-color: #131232;
        box-shadow: 0.5px 0.5px 1px 0px #2E296B inset;
        border-radius: 10px;
        padding: 10px 15px;
        margin-bottom: 10px;
        position: relative;

        .member_info {
          display: flex;
          align-items: center;
          

          .member_avatar {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            margin-right: 10px;
          }

          .info {
            display: flex;
            flex-direction: column;

            .name_section {
              display: flex;
              align-items: center;
              gap: 8px;
              

              .member_name {
                font-size: 14px;
                font-weight: bold;
                margin: 0;
                color: #FFFFFF;
              }
              .tag {
                width: 45px;
                height: 36px;
                line-height: 32px;
                font-size: 10px;
                border-radius: 6px;
                color: #fff;
                position: absolute;
                left: 0;
                top: 0;
                z-index: 99;

                 .tag_text {
                  display: inline-block;
                  transform: rotate(-40deg);
                 }
              }

              .tag.creator {
                background: url(/imgs/club/cjz.png) no-repeat;
                background-size: 100% 100%;
              }

              .tag.admin {
                background: url(/imgs/club/gly.png) no-repeat;
                background-size: 100% 100%;
              }
            }

            .vip_tag {
              margin-top: 5px;

              img {
                width: 50px;
                height: 20px;
              }
            }
          }
        }

        .member_actions {
          display: flex;
          gap: 10px;

          .action_btn {
            height: 32px;
            line-height: 32px;
            font-size: 12px;
            text-align: center;
            color: #C6C4F5;
            padding: 0 8px;
            background: #282747;
            box-shadow: 0.5px 0.5px 1px 0px #C6C4F5 inset;
            border-radius: 8px;
          }
        }
      }
    }


  }
  .popup_body {
  color: #fff;
  padding: 10px;

  .info {
    margin-bottom: 10px;
    text-align: center;
    color: #FFFFFF;
    margin: 24px 0;
  }

  .submit_btn {
    margin: 20px 0;
    display: flex;
    justify-content: space-evenly;

    .submit_close {
      width: 147px;
      height: 42px;
      color: #FFFFFF;
      text-align: center;
      line-height: 44px;
      background: #282747;
      box-shadow: 0.5px 0.5px 1px 0px #2E296B inset;
      border-radius: 8px;
    }
  }
}
  </style>
  